(function(){

'use strict';

angular.module('controllers.oldOMDisplay.infoDispaly')
	.controller('developeAnalysisController', developeAnalysisController);

function developeAnalysisController($scope, InfoViewService, COREDATA, CoreService, CoreLoading) {
	$scope.errorInfo = COREDATA.ERRORINFO; //日期错误信息
	//无请求写死的数据 时间跨度下拉数据
	$scope.timeTypes = [{
		timeTypeCode: '2',
		timeTypeName: '周'
	}, {
		timeTypeCode: '3',
		timeTypeName: '月'
	}, {
		timeTypeCode: '4',
		timeTypeName: '季'
	}];
	//省份数据
	$scope.provinces = COREDATA.PROVINCES;
	//业务线
	$scope.businessList = COREDATA.BUSINESSLINE;
	$scope.shoumun = 1;
	//查询条件参数
	$scope.translate = {
		provinceCode: [],
		businessCode: '',
		timeTypeCode: {
			timeTypeCode: '3',
			timeTypeName: '月'
		},
		startTime: new Date(),
		startQuarter: {
			code: '1',
			quarterName: '第一季度'
		},
		endTime: new Date(),
		endQuarter: {
			code: '1',
			quarterName: '第一季度'
		},
		year: {}
	};

	/**
	 * [ 设置账期起始时间的默认值]
	 * 初始化页面值的默认 查询账期为日
	 * @param {[type]} timeTypeCode [description]
	 */
	$scope.$watch('translate.timeTypeCode', function(newValue) {
		var month = new Date().getMonth(); //当前月份
		var day = new Date().getDate(); //当前天
		var year = new Date().getFullYear(); //当前年份
		switch (newValue.timeTypeCode) {
			case '2':
				$scope.translate.startTime = new Date(year, month, day - 1);
				$scope.translate.endTime = new Date(year, month, day - 1);
				break;
			case '3':
				$scope.translate.startTime = new Date(year, month);
				$scope.translate.endTime = new Date(year, month);
				break;
			case '4':
				$scope.translate.year.startTime = '';
				$scope.translate.year.endTime = '';
				break;
		}
	});

	$scope.queryAllChannel = function() {
		var paramsData = {};
		$scope.shoumun == 1 ? $scope.shoumun = 0 : $scope.shoumun = 1;
		if ($scope.shoumun === 0) {
			CoreLoading.setState(true);
			InfoViewService.queryAllChannel(paramsData, queryAllChannel);
		}

	};

	$scope.searchChannel = function() {
		var paramsData = {};
		paramsData.provinceCodes = $scope.translate.provinceCode.map(function(item){
			return item.provinceCode;
		});
		paramsData.timeType = $scope.translate.timeTypeCode.timeTypeCode;
		$scope.shoumun == 1 ? $scope.shoumun = 0 : $scope.shoumun = 1;
		if ($scope.shoumun === 0) {
			CoreLoading.setState(true);
			$scope.searchInfo = angular.copy($scope.translate);
			InfoViewService.queryChannelData(paramsData, searchChannel);
		}
	};
	var catChart, moveChart, ataChart;
	catChart = echarts.init(document.getElementById('cat'));
	moveChart = echarts.init(document.getElementById('move'));
	ataChart = echarts.init(document.getElementById('ata'));

	function searchChannel(res) {
		CoreLoading.setState(false);
		if (res.state === '10000' && res.data.length > 0) {
			$scope.flagObj = true;
			var skyCatOption = new ChartOption('天猫渠道发展客户分析', res.data[0].channelData);
			var moveOption = new ChartOption('移动商城渠道发展客户分析', res.data[1].channelData);
			var bankOption = new ChartOption('银行总对总渠道发展客户分析', res.data[2].channelData);
            catChart.clear();
            moveChart.clear();
            ataChart.clear();
			catChart.setOption(skyCatOption);
			moveChart.setOption(moveOption);
			ataChart.setOption(bankOption);

		} else {
			CoreService.dialog(res.state);
		}
	}

	function ChartOption(title, data) {
		this.title = {
			text: title,
			x: 'center'
		};
		this.tooltip = {
			trigger: 'item',
			formatter: "{a} <br/>{b} : {c} ({d}%)"
		};
		this.legend = {
			orient: 'vertical',
			x: 'left',
			data: ['1-10%', '10-20%', '20-30%', '30-40%', '40-50%', '50-60%', '60-70%', '70-80%', '80-90%', '90-100%']
		};
		this.series = [{
			name: '客户偏好概率',
			type: 'pie',
			radius: '50%',
			center: ['50%', '50%'],
			data: data
		}];
	}
	$scope.searchChannel();
}

})();